package com.ewhl.mark.meijialian;

import android.content.Context;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.ewhl.mark.meijialian.volley.BaseVO;
import com.ewhl.mark.meijialian.volley.MyErrorListener;
import com.ewhl.mark.meijialian.volley.MyReponseListener;
import com.zhy.autolayout.AutoRelativeLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CompleteRegisterActivity extends AppCompatActivity {
    @BindView(R.id.return_iv)
    AutoRelativeLayout returnIv;
    @BindView(R.id.et_new_phone)
    EditText etNewPhone;
    @BindView(R.id.et_sms_code)
    EditText etSmsCode;
    @BindView(R.id.btn_get_code)
    TextView btnGetCode;
    @BindView(R.id.btn_save)
    AutoRelativeLayout btnSave;
    String phone;
    Context mContext;
    String code;
    private CountDownTimer timer;
    NetValues netValues;

    String nickname="";
    String header_pic="";
    String oauth="";
    String openid="";
    int sex;
    String url="http://chengjing.cnewhl.com/index.php/Mobile/Index/loadding";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_complete_reg);
        ButterKnife.bind(this);
        mContext=this;
        netValues=NetValues.getInstance(this);
        nickname=getIntent().getStringExtra("nickname");
        header_pic=getIntent().getStringExtra("header");
        oauth=getIntent().getStringExtra("oauth");
        openid=getIntent().getStringExtra("openid");
        sex=getIntent().getIntExtra("sex",-1);
        timer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long l) {
                btnGetCode.setText("重新获取(" + (l / 1000) + ")");
                btnGetCode.setEnabled(false);
            }

            @Override
            public void onFinish() {
                btnGetCode.setEnabled(true);
                btnGetCode.setText("获取验证码");
            }
        };
    }

    @OnClick({R.id.return_iv, R.id.btn_get_code, R.id.btn_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.return_iv:
                finish();
                break;
            case R.id.btn_get_code:
                getsms_code();
                break;
            case R.id.btn_save:
                phone=etNewPhone.getText().toString();
                code=etSmsCode.getText().toString();
                if(!CommonUtils.checkNull(code)){
                    Toast.makeText(mContext, "请填写短信验证码", Toast.LENGTH_SHORT).show();
                    return;
                }
               netValues.user_third_reg(nickname, header_pic, phone, sex, openid, oauth, code, new MyReponseListener() {
                   @Override
                   public void onResponse(BaseVO arg0) {
                      LoginBean loginBean=(LoginBean) arg0;
                    if(loginBean.getStatus()==200){
                        SPUtils.put(mContext, "key", loginBean.result);
                        CommonUtils.goToWeb(mContext,CommonWebActivity.class,url+"/key/"+loginBean.result);
                        CommonUtils.destoryActivity();
                        finish();
                    }
                   }
               }, new MyErrorListener() {
                   @Override
                   public void onErrorResponse(VolleyError error) {
                       super.onErrorResponse(error);
                   }
               });
                break;
        }
    }
    public void getsms_code() {
        phone=etNewPhone.getText().toString();
        if(!CommonUtils.checkNull(phone)){
            if(!CommonUtils.isMobileNO(phone)){
                Toast.makeText(this, "请填写正确的手机号", Toast.LENGTH_SHORT).show();
                return;
            }
            Toast.makeText(mContext, "请填写手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        timer.onTick(60000);
        timer.start();
        netValues.send_sms(phone, "6",new MyReponseListener() {
            @Override
            public void onResponse(BaseVO arg0) {
                super.onResponse(arg0);
                OptionBean smsbean= (OptionBean) arg0;
                if(smsbean.status==1){
                    Toast.makeText(mContext, "发送成功", Toast.LENGTH_SHORT).show();

                }else {
                    Toast.makeText(mContext, "发送失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public boolean onMyResponse(BaseVO t) {
                return super.onMyResponse(t);
            }
        }, new MyErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                super.onErrorResponse(error);
            }
        });

    }
}
